/**
 * 统一日志系统
 * 支持不同级别的日志记录和环境控制
 */

export enum LogLevel {
  DEBUG = 0,
  INFO = 1,
  WARN = 2,
  ERROR = 3,
  NONE = 4
}

interface LogEntry {
  level: LogLevel
  message: string
  data?: any
  timestamp: string
  source?: string
}

class Logger {
  private level: LogLevel
  private isDevelopment: boolean
  private logs: LogEntry[] = []
  private maxLogs = 1000

  constructor() {
    this.isDevelopment = import.meta.env.DEV
    this.level = this.isDevelopment ? LogLevel.DEBUG : LogLevel.WARN
  }

  private shouldLog(level: LogLevel): boolean {
    return level >= this.level
  }

  private formatMessage(level: LogLevel, message: string, source?: string): string {
    const levelNames = ['DEBUG', 'INFO', 'WARN', 'ERROR']
    const timestamp = new Date().toISOString()
    const sourceStr = source ? `[${source}]` : ''
    return `[${timestamp}] ${levelNames[level]} ${sourceStr} ${message}`
  }

  private addLog(level: LogLevel, message: string, data?: any, source?: string) {
    const logEntry: LogEntry = {
      level,
      message,
      data,
      timestamp: new Date().toISOString(),
      source
    }

    this.logs.push(logEntry)
    
    // 保持日志数量在限制内
    if (this.logs.length > this.maxLogs) {
      this.logs = this.logs.slice(-this.maxLogs)
    }
  }

  debug(message: string, data?: any, source?: string) {
    if (!this.shouldLog(LogLevel.DEBUG)) return

    this.addLog(LogLevel.DEBUG, message, data, source)
    
    if (this.isDevelopment) {
      const formattedMessage = this.formatMessage(LogLevel.DEBUG, message, source)
      if (data) {
        console.log(formattedMessage, data)
      } else {
        console.log(formattedMessage)
      }
    }
  }

  info(message: string, data?: any, source?: string) {
    if (!this.shouldLog(LogLevel.INFO)) return

    this.addLog(LogLevel.INFO, message, data, source)
    
    if (this.isDevelopment) {
      const formattedMessage = this.formatMessage(LogLevel.INFO, message, source)
      if (data) {
        console.info(formattedMessage, data)
      } else {
        console.info(formattedMessage)
      }
    }
  }

  warn(message: string, data?: any, source?: string) {
    if (!this.shouldLog(LogLevel.WARN)) return

    this.addLog(LogLevel.WARN, message, data, source)
    
    const formattedMessage = this.formatMessage(LogLevel.WARN, message, source)
    if (data) {
      console.warn(formattedMessage, data)
    } else {
      console.warn(formattedMessage)
    }
  }

  error(message: string, error?: any, source?: string) {
    if (!this.shouldLog(LogLevel.ERROR)) return

    this.addLog(LogLevel.ERROR, message, error, source)
    
    const formattedMessage = this.formatMessage(LogLevel.ERROR, message, source)
    if (error) {
      console.error(formattedMessage, error)
    } else {
      console.error(formattedMessage)
    }
  }

  // 获取日志历史
  getLogs(level?: LogLevel): LogEntry[] {
    if (level !== undefined) {
      return this.logs.filter(log => log.level >= level)
    }
    return [...this.logs]
  }

  // 清除日志
  clearLogs() {
    this.logs = []
  }

  // 设置日志级别
  setLevel(level: LogLevel) {
    this.level = level
  }

  // 导出日志
  exportLogs(): string {
    return this.logs.map(log => {
      const levelNames = ['DEBUG', 'INFO', 'WARN', 'ERROR']
      const dataStr = log.data ? ` | Data: ${JSON.stringify(log.data)}` : ''
      const sourceStr = log.source ? ` | Source: ${log.source}` : ''
      return `${log.timestamp} [${levelNames[log.level]}]${sourceStr} ${log.message}${dataStr}`
    }).join('\n')
  }
}

// 创建全局日志实例
export const logger = new Logger()

// 便捷的日志函数
export const log = {
  debug: (message: string, data?: any, source?: string) => logger.debug(message, data, source),
  info: (message: string, data?: any, source?: string) => logger.info(message, data, source),
  warn: (message: string, data?: any, source?: string) => logger.warn(message, data, source),
  error: (message: string, error?: any, source?: string) => logger.error(message, error, source)
}

export default logger 